package com.wwq.learn_jpa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.wwq.learn_jpa.dao.RightsCategoryDAO;
import com.wwq.learn_jpa.exception.RightsCategoryException;
import com.wwq.learn_jpa.model.dto.RightsCategoryDTO;
import com.wwq.learn_jpa.model.entity.RightsCategory;
import com.wwq.learn_jpa.model.enums.ExceptionMessageType;
import com.wwq.learn_jpa.service.RightsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:wwq
 * @desc:
 * @date 2022/3/17
 */
@Service
public class RightsCategoryServiceImpl implements RightsCategoryService {

    @Autowired
    private RightsCategoryDAO rightsCategoryDAO;

    /**
     * 权益的新增
     * @param rcDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addAndUpdate(RightsCategoryDTO rcDTO) {
        if (ObjectUtil.isNull(rcDTO)) {
            throw new RightsCategoryException(ExceptionMessageType.MESSAGE_IS_NULL.getMessage());
        }
        RightsCategory rc = BeanUtil.toBean(rcDTO, RightsCategory.class);
        return ObjectUtil.isNull(rightsCategoryDAO.save(rc))?false:true;
    }

    /**
     * 通过id获取权益
     * @param id
     * @return
     */
    @Override
    public RightsCategoryDTO findById(Long id) {
        return null;
    }

    /**
     * 分页查询获取权益列表
     * @return
     */
    @Override
    public Page<RightsCategory> page(Pageable pageable) {
        return rightsCategoryDAO.findAll(pageable);
    }

}
